import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/common/app.dart';
import 'package:flutter_smart_heating/config/config.dart';
import 'package:flutter_smart_heating/ui/ui.dart';
import 'package:get/get.dart';

class AgreementView extends StatelessWidget {
  const AgreementView({
    super.key,
    this.isAgree = false,
    this.showCheckbox = true,
    this.iconNormalColor = Colors.grey,
    this.iconCheckedColor = Colors.blue,
    this.textColor,
    this.fontSize,
    this.linkColor,
    this.list = const [],
    this.onTap,
  });

  final bool showCheckbox;
  final bool isAgree;
  final Color iconNormalColor;
  final Color iconCheckedColor;
  final Color? textColor;
  final double? fontSize;
  final Color? linkColor;

  final Function(bool isAgree)? onTap;
  final List<TextSpan> list;
  @override
  Widget build(BuildContext context) {
    return RichText(
      text: TextSpan(
        style: TextStyle(
          fontSize: fontSize,
          height: 1.6,
          color: textColor ?? UiTheme(context).infoColor,
        ),
        children: [
          if (showCheckbox)
            WidgetSpan(
              alignment: PlaceholderAlignment.middle,
              child: GestureDetector(
                onTap: () {
                  onTap?.call(!isAgree);
                },
                child: Icon(
                  isAgree ? Icons.check_circle : Icons.circle_outlined,
                  size: 18,
                  color: isAgree ? iconCheckedColor : iconNormalColor,
                ),
              ),
            ),
          if (showCheckbox)
            const WidgetSpan(
              child: SizedBox(
                width: 5,
              ),
            ),
          TextSpan(
            text: '已阅读并同意'.tr,
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                onTap?.call(!isAgree);
              },
          ),
          TextSpan(
            text: '《用户协议》',
            style: TextStyle(color: linkColor ?? UiTheme.primary),
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                App.push(
                  'browser://plugin/page/index/detail.html?name=agreement&title=用户协议',
                  context: context,
                );
              },
          ),
          const TextSpan(
            text: '和',
          ),
          TextSpan(
            text: '《隐私政策》',
            style: TextStyle(color: linkColor ?? UiTheme.primary),
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                App.push(
                  'browser://plugin/page/index/detail.html?name=policy&title=隐私政策',
                  context: context,
                );
              },
          ),
          ...list,
        ],
      ),
    );
  }
}

class AgreementDialog extends StatelessWidget {
  const AgreementDialog({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 20),
        child: Material(
            color: Colors.white,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(UiTheme.boxRadiusX)),
            child: Container(
              padding: const EdgeInsets.only(left: 20, right: 20, top: 20),
              constraints: const BoxConstraints(maxWidth: 280),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Center(
                    child: Text(
                      '个人隐私保护指引',
                      textScaler: TextScaler.linear(1.2),
                      style: TextStyle(
                          fontWeight: FontWeight.bold, color: Colors.black87),
                    ),
                  ),
                  const SizedBox(height: 10),
                  const AgreementView(
                    showCheckbox: false,
                    textColor: Colors.black54,
                  ),
                  const SizedBox(height: 10),
                  Text(
                    '${Config.appName}将严格保护您的个人信息安全',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.black.withOpacity(0.7),
                    ),
                  ),
                  const SizedBox(height: 15),
                  SizedBox(
                    height: UiTheme.buttonHeight - 5,
                    width: double.infinity,
                    child: UiButton(
                      onPressed: () {
                        App.back(context, result: true);
                      },
                      child: const Text(
                        '同意并登录',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                  SizedBox(
                    height: UiTheme.buttonHeight,
                    width: double.infinity,
                    child: TextButton(
                      style: ButtonStyle(
                        overlayColor: Ui.createTextBtnBgColor(),
                      ),
                      onPressed: () {
                        App.back(context);
                      },
                      child: const Text(
                        '不同意',
                        style: TextStyle(color: Colors.black54),
                      ),
                    ),
                  ),
                ],
              ),
            )),
      ),
    );
  }
}
